package componentInit

import (
	mq "db2s/topic-mq"
	"os"
	"strings"
	"time"
)

type (
	Bar struct {
		TaskTotal        *int64 //任务总数
		Task             int    //任务数
		Free             *int64
		BreakPointSum    *int64
		BeginTime        time.Time //开始时间
		CheckMod         string    //模式
		Object           string    //索引、无所引
		XlsDataSheetName string
		TtyBarFin        *os.File
	}
)

func (b Bar) GetIndexMode() CheckMode {
	switch b.CheckMod {
	case string(Count):
		return CountM
	case string(Rows):
		if strings.EqualFold(b.Object, "index") {
			return RowsIndex
		} else if strings.EqualFold(b.Object, "miss") {
			return RowsNoIndex
		} else {
			return RowsM
		}
	case string(SyncRows):
		return SyncRowsM
	default:
		return RowsM
	}
}

func (b Bar) newBar(l int /*topic长度*/) *mq.TaskBar {
	return &mq.TaskBar{
		BeginTime:         b.BeginTime,
		Total:             b.TaskTotal,
		Accumulate:        0,
		Free:              b.Free,
		BreakPointSum:    b.BreakPointSum,
		Ctx:               make(chan int),
		CheckMod:          b.CheckMod,
		FinishFile:        b.TtyBarFin,
		SubTasks:          make(chan *mq.SubTasks, l*b.Task),
		SpeedLimitMonitor: make(chan mq.SpeedLimitMonitor, l*b.Task),
		FinishResultC:     make(chan any, l*b.Task),
	}
}

func InitBarMonitor(b Bar, l int /*topic长度*/) *mq.TaskBar {
	return b.newBar(l)
}
